<!DOCTYPE html>
<!--[if IE 8]> <html lang="en" class="ie8 no-js"> <![endif]-->
<!--[if IE 9]> <html lang="en" class="ie9 no-js"> <![endif]-->
<!--[if !IE]><!-->
<html lang="en">
<!--<![endif]-->
<!-- BEGIN HEAD -->

<head>
    <meta charset="utf-8" />
    <title></title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta content="width=device-width, initial-scale=1" name="viewport" />
    <meta content="" name="description" />
    <meta content="" name="author" />
    <!-- Bootstrap 3.3.6 -->
    <link rel="stylesheet" href="http://tv.speechb.com/lib/superui/content/ui/global/bootstrap/css/bootstrap.min.css">
    <!-- Font Awesome -->
    <link href="http://tv.speechb.com/lib/superui/content/ui/global/font-awesome/css/font-awesome.css" rel="stylesheet" />
    <!-- Theme style -->
    <link rel="stylesheet" href="http://tv.speechb.com/lib/superui/content/adminlte/dist/css/AdminLTE.css">
    <link rel="stylesheet" href="http://tv.speechb.com/lib/superui/content/adminlte/dist/css/skins/_all-skins.min.css">
    <link href="http://tv.speechb.com/lib/superui/content/min/css/supershopui.common.min.css" rel="stylesheet" />
    <link href="http://tv.speechb.com/lib/superui/content/plugins/bootstrap-table/bootstrap-table.css" rel="stylesheet" />
    <link href="http://tv.speechb.com/lib/superui/content/layx/layx.min.css" rel="stylesheet" />
    <link href="http://tv.speechb.com/lib/x-editable/css/bootstrap-editable.css" rel="stylesheet"/>
    <link href="http://tv.speechb.com/lib/x-editable/css/select2-bootstrap.css" rel="stylesheet"/>
    <link href="http://tv.speechb.com/lib/select2/css/select2.min.css" rel="stylesheet"/>
    <!--全局通用框架样式 end-->


</head>
<body>
<!--
<script id="editor" type="text/plain" style="height: 150px;"/>

-->
<!-- Main content -->
<div class="content" style="margin-top: 10px">
<div class="box box-info">
  <h4 class="box-header with-border" >话术信息</h4>
</div>
<div class="box-body">
    <div class="form-group">
        <label for="name">文本框</label>
        <textarea id="content" class="form-control" rows="12"></textarea>
    </div>
    <div class="box-body">
    <div class="row">
        <div class="col-md-3">
            <div class="form-group">
                <label>话术主标题</label>
                <select id="title1_select" name="parentid" class="form-control">
                    <option selected="selected" value="0">--请选择--</option>
                </select>
            </div>
        </div>
        <div class="col-md-3">
            <div class="form-group">
                <label>话术层标题</label>
                <select id="title2_select"  name="parentid" class="form-control">
                    <option selected="selected" value="0">--请选择--</option>
                </select>
            </div>
        </div>
        <div class="col-md-3">
            <div class="form-group">
                <label>发布类型</label>
                <select id="publish_type" name="parentid" class="form-control">
                    <option selected="selected" value="1">官方发布</option>
                    <option  value="2">用户发布</option>
                </select>
            </div>
        </div>

        <div class="col-md-3">
        </div>

</div>
        </div>
        <button id="submitBtn" type="button"  class="btn btn-primary center-block" style="margin-top:18px"> 发表话术 </button>

        <script src="http://tv.speechb.com/lib/superui/content/ui/global/jQuery/jquery.min.js"></script>

<!-- Bootstrap 3.3.6 -->
<script src="http://tv.speechb.com/lib/superui/content/ui/global/bootstrap/js/bootstrap.min.js"></script>
<script src="http://tv.speechb.com/lib/select2/js/select2.full.min.js"></script>

<script src="http://tv.speechb.com/lib/x-editable/js/bootstrap-editable.min.js"></script>

<script src="http://tv.speechb.com/lib/superui/content/min/js/supershopui.common.js"></script>
<script src="http://tv.speechb.com/lib/superui/content/plugins/bootstrap-table/bootstrap-table.js"></script>
<script src="http://tv.speechb.com/lib/superui/content/plugins/bootstrap-table/locale/bootstrap-table-zh-CN.js"></script>
<script src="http://tv.speechb.com/lib/superui/content/layx/layx.min.js"></script>

    <!--建议手动加在语言，避免在ie下有时因为加载语言失败导致编辑器加载失败-->
    <!--这里加载的语言文件会覆盖你在配置项目里添加的语言类型，比如你在配置项目里配置的是英文，这里加载的中文，那最后就是中文-->
    <script type="text/javascript" charset="utf-8" src="/static/ueditor-dev-1.5.0/lang/zh-cn/zh-cn.js"></script>
    <script type="text/javascript" charset="utf-8" src="http://tv.speechb.com/lib/toaster/jquery.toaster.js"></script>
    <script type="text/javascript">


    $(function(){
        $(".select2").select2();
        function initSelect2(id,url) {
            $(id).select2({
                language: "zh-CN",// 指定语言为中午，国际化才能起效
                inputMessage: "请选择",
                pagination:{
                    more:true
                },
                ajax: {
                    url: url,
                    dataType: 'json',
                    delay:250, // 延迟显示
                    data: function (params) {
                        var query = {
                            search: params.term,
                            page: params.page || 1,
                            title1_id: $("#title1_select").val(),
                            rows: 10 //每页显示多少行
                        }

                        // Query parameters will be ?search=[term]&page=[page]
                        return query;
                    },
                    // 分页
                    processResults: function (data, params) {
                        params.page = data.next_page-1;
                        return {
                            results: data.data,// 后台返回数据
                            pagination: {
                                more: (params.page)*10<data.total// 总数
                            }
                        };
                    },
                    cache: false
                },
                escapeMarkup: function (markup) {
                    return markup;
                },
                minimumInputLength: 0,// 0长度及搜索所以
                templateResult: function(repo){// 显示的结果集格式，需要自己写CSS
                    // 正在检索
                    if(repo.loading) return repo.text;
                    var markup = repo.text;
                    var markup = "";
                    markup +="<div class ='select2-result-repository_statistics'>"
                        +"<div class='select2-result-repository_forks'><i class = 'fa fa-bolt'></i> &nbsp;"+repo.text+"</div>"
                        +"</div>";
                    return markup;
                },
                templateSelection: function(repo) {
                    // 选中某一列后文本框
                    return "<div>" +repo.text || repo.id + "</div>";
                },
                placeholder: '请选择'
            });
        }
        function initTitle1() {
            initSelect2("#title1_select","/speech_craft_title/select?type=1");
           // $("#title1_select").html('<option value="0">_请选择_</option>').trigger("change");
            $("#title1_select").off().on("change",function(){
                var data = $(this).val();
            });
            $("#title1_select").append(new Option("--请选择标题--", 0, true, true));//第一个参数时id,第二个参数是text
            $("#title1_select").trigger("change");


        }
        function initTitle2() {
            initSelect2("#title2_select","/speech_craft_title/select?type=2");

        }
        initTitle1();
        initTitle2();
        $("#submitBtn").click(function () {
            var title2 = $("#title2_select").val();
            var content = $("#content").val();
            var publishType = $("#publish_type").val();

            publishSpeechcraft(title2,content,publishType);
        });
        $("#content").bind('input propertychange', function() {
            var title2 = $("#title2_select").val();
            var content = $("#content").val();
            var publishType = $("#publish_type").val();

            publishSpeechcraft(title2,content,publishType);
        })
        function publishSpeechcraft(title2,content,publishType) {
            $.ajax({
                //请求方式
                type : "POST",
                //请求地址
                url : "/speech_craft/add_all3",
                //数据，json字符串
                data : {
                    "title2_id":title2,
                    "content":content,
                    "publish_type":publishType
                },
                //请求成功
                success : function(result) {
                    if(result.code==1) {
                        $("#content").val("");
                        /* localStorage.setItem("username",username);
                         localStorage.setItem("password",password);*/
                        $.toaster({ priority : "success", title : "通知", message : "发布话术成功,数量:"+result.data });
                    } else {
                        $.toaster({ priority : "danger", title : "通知", message : "发布话术失败" })
                    }
                    console.log(result);
                },
                error : function(e){
                    $.toaster({ priority : "danger", title : "通知", message : "发布话术失败" })
                }
            });
        }

        $("#publish_type").select2();

        function getQueryVariable(variable)
        {
            var query = window.location.search.substring(1);
            var vars = query.split("&");
            for (var i=0;i<vars.length;i++) {
                var pair = vars[i].split("=");
                if(pair[0] == variable){return pair[1];}
            }
            return null;
        }
        var id = null;
        function dealUpdate() {
            id = getQueryVariable("id");
            console.log(id);
            if(id == null) {
                console.log("更新");
                return;
            }
            $.ajax({
                type : "GET",
                url : "/admin/speechcraft/update_inf",
                data : {
                    "id":id,
                },
                success : function(result) {
                    if(result.code==1) {
                        $("#title2_select").append(new Option(result.data.title, result.data.speechcraft_title2_id, true, true));//第一个参数时id,第二个参数是text
                        $("#title2_select").trigger("change");
                        var text;
                        if(result.data.post_type_id ==1) {
                            text = "官方发布";
                        } else if(result.data.post_type_id ==2) {
                            text = "用户发布"
                        } else {
                            text ="其它";
                        }

                        $("#publish_type").append(new Option(text, result.data.post_type_id, true, true));//第一个参数时id,第二个参数是text
                        $("#publish_type").trigger("change");
                        window.setTimeout(setContent,1000,result.data.content);//一秒后再调用赋值方法
                    } else {
                        $.toaster({ priority : "danger", title : "通知", message : "请求话术失败" })
                    }
                    console.log(result);
                },
                error : function(e){
                    $.toaster({ priority : "danger", title : "通知", message : "请求话术失败" })
                }
            });
        }
        dealUpdate();

        //给ueditor插入值
        function setContent(content){
            console.log(content)
            UE.getEditor('editor').execCommand('insertHtml',content);
        }
        var _setTimeout=setTimeout;
        window.setTimeout=function(callback,time,params){
            var param=Array.prototype.slice.call(arguments,2);
            var _callback=function(){
                callback.apply(null,param);
            }
            _setTimeout(_callback,time);
        }
    });

</script>
</body>

</html>